home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
- From: Dan Pop <danpop@mail.cern.ch>
- Newsgroups: comp.lang.c
- Subject: Re: quick decision: is n a power of 2?
- Date: Sun, 21 Jan 1996 12:03:59 +0100
- Organization: CERN European Lab for Particle Physics
- Message-ID: <9601211103.AA19325@dxmint.cern.ch>
- References: <4dpd94$c25@fountain.mindlink.net> <4dsaco$a39@newsbf02.news.aol.com>
- X-NNTP-Posting-Host: hpl3sn03.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
- X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
-
- babycox@aol.com (BabyCox) writes:
-
- >Here is a revision, this should work (but that's what I said last time)
-
- You're a little bit closer this time :-)
-
- >Boolean isPowerOfTwo(long x)
- >{
- > long tst;
- > for(tst=1<<31;tst!=0;tst>>=1)
- > if (x==tst) return true;
- >
- > return false;
- >}
-
- I've already commented about the usage of the undefined Boolean, true and
- false.
-
- This new version will work _only_ on some implementations. To make it
- portable, tst has to be unsigned long (x should also be unsigned long
- because negative numbers are _never_ powers of two :-)
-
- If your C book hasn't taught you that the behaviour of the >> operator is
- implementation-defined when the first argument is negative, then it's time
- to read K&R2.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-